#include "handle_msg.h"

Status getLoginMsg(LoginMsg* msg, int flag) {
	int t;

	if (flag & L_Account)
	{
		fprintf(stderr, "请输入账户名[最长%d个字符]：", MAX_ACCOUNT_SIZE-1);
		t = read(STDIN_FILENO, msg->account, MAX_ACCOUNT_SIZE);
		if (t <= 0) {
			INPUT_ERR;
			return Failure;
		}
		if (msg->account[t-1] != '\n') {
			while(getchar() != '\n');
		}
		msg->account[t-1] = '\0';
	}
	
	if (flag & L_Passwd)
	{
		fprintf(stderr, "请输入密码[最大%d个字符]: ", MAX_PASSWD_SIZE-1);
		t = read(STDIN_FILENO, msg->passwd, MAX_PASSWD_SIZE);
		if (t <= 0) {
			INPUT_ERR;
			return Failure;
		}
		if (msg->passwd[t-1] != '\n') {
			while(getchar() != '\n');
		}
		msg->passwd[t-1] = '\0';
	}

	if (flag & L_aType)
	{
		t = -1;
		fprintf(stderr, "请输入账户类型[0 管理员，1 普通用户]：");
		scanf("%d", &t);
		while(getchar() != '\n');
		switch (t)
		{
			case 0:
				msg->ID.a_type = Admin;
				break;
			case 1:
				msg->ID.a_type = Normal;
				break;
			default:
				CHOOSE_ERR;
				return Failure;
				break;
		}
	}
	return Success;
}

Status showStaffMsg(const StaffMsg* msg, int flag) {
	if (flag & S_uID) {
		printf("%d, ", msg->ID.u_id);
	}
	if (flag & S_Name) {
		printf("%s, ", msg->name);
	}
	if (flag & S_Age) {
		printf("%d, ", msg->age);
	}
	if (flag & S_Tel) {
		printf("%d, ", msg->tel);
	}
	putchar('\n');
	return Success; 
}

Status getStaffMsg(StaffMsg* msg, int flag) {
	int t;

	if (flag & S_uID) {
		t = -1;
		fprintf(stderr, "请输入工号[非负数]:");
		scanf("%d", &t);
		while(getchar() != '\n');
		if (t < 0) {
			INPUT_ERR;
			return Failure;
		}
		msg->ID.u_id = t;
	}

	if (flag & S_Name) {
		fprintf(stderr, "请输入姓名[最大长度不超过%d个字符]: ", MAX_NAME_SIZE-1);
		t = read(STDIN_FILENO, msg->name, MAX_NAME_SIZE);
		if (t <= 0) {
			INPUT_ERR;
			return Failure;
		}
		if (msg->name[t-1] != '\n') {
			while(getchar() != '\n');
		}
		msg->name[t-1] = '\0';
	}

	if (flag & S_Age) {
		t = -1;
		fprintf(stderr, "请输入年龄[非负数]:");
		scanf("%d", &t);
		while(getchar() != '\n');
		if (t < 0) {
			INPUT_ERR;
			return Failure;
		}
		msg->age = t;
	}

	if (flag & S_Tel) {
		t = -1;
		fprintf(stderr, "请输入电话[非负数]:");
		scanf("%d", &t);
		while(getchar() != '\n');
		if (t < 0) {
			INPUT_ERR;
			return Failure;
		}
		msg->tel = t;
	}
	return Success;
}

Status showHistoryMsg(const HistoryMsg* msg) {
	printf("%d  %s  %s\n", msg->ID.a_id, msg->action, msg->my_time);
	return Success;
}

Status chooseItems(int* flag) {
	int t;

	while (1)
	{
		putchar('\n');
		PRINT_SPLITE;
		printf("选择您想要标记的列[-1 退出选项菜单]：\n");
		printf("0:全部, %s\n", HEADER);
		PRINT_SPLITE;
		
		t = -2;
		scanf("%d", &t);
		while(getchar() != '\n');
		
		switch (t)
		{
			case -1:
				return Success;
				break;
			case 0:
				*flag |= S_All;
				return Success;
				break;
			case 1:
				*flag |= S_uID;
				break;
			case 2:
				*flag |= S_Name;
				break;
			case 3:
				*flag |= S_Age;
				break;
			case 4:
				*flag |= S_Tel;
				break;
			default:
				CHOOSE_ERR;
				break;
		}
	}
	return Success;
}

